package com.sl.biz.modular.process.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.process.controller.request.ProcessRunnerKillRequest;
import com.sl.biz.modular.process.controller.request.ProcessRunnerStopRequest;
import com.sl.biz.modular.process.controller.response.ProcessRunnerInfoVO;
import com.sl.biz.modular.process.service.ProcessRunnerInfoBizService;
import com.sl.core.engine.ProcessEngine;
import com.sl.core.engine.trans.ProcessInstance;
import lombok.RequiredArgsConstructor;
import org.apache.hop.pipeline.Pipeline;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class ProcessRunnerInfoBizServiceImpl implements ProcessRunnerInfoBizService {
    @Override
    public List<ProcessRunnerInfoVO> list() {

        List<ProcessInstance> activeProcessTrans = ProcessEngine.INSTANCE.getActiveProcessTrans();

        //所有正在运行的
        List<ProcessInstance> collect = activeProcessTrans.stream().filter(Pipeline::isRunning).toList();


        return collect.stream().map(v -> {
            ProcessRunnerInfoVO processRunnerInfoVO = new ProcessRunnerInfoVO();
            processRunnerInfoVO.setProcessName(v.getProcessName());
            processRunnerInfoVO.setProcessVersion(v.getProcessVersion());
            processRunnerInfoVO.setProcessId(v.getProcessId());
            processRunnerInfoVO.setTid(v.getTid());
            processRunnerInfoVO.setStatus(v.getStatusDescription());
            processRunnerInfoVO.setFastFailFlag(v.getFastFailFlag());
            processRunnerInfoVO.setDescription(v.getDescription());
//            Result result = v.getResult();
//            processRunnerInfoVO.setResult(result);

//            PipelineEngineCapabilities engineCapabilities = v.getEngineCapabilities();
//            processRunnerInfoVO.setCapabilities(engineCapabilities);

//            EngineMetrics engineMetrics = v.getEngineMetrics();
//            processRunnerInfoVO.setEngineMetrics(engineMetrics);

            String input = null;
            try {
                input = v.getParameterValue("input");
            } catch (Exception e) {
            }
            String output = null;
            try {
                output = v.getParameterValue("output");
            } catch (Exception e) {

            }
            processRunnerInfoVO.setInput(input);
            processRunnerInfoVO.setOutput(output);
            processRunnerInfoVO.setStartDate(v.getExecutionStartDate());
            processRunnerInfoVO.setEndDate(v.getExecutionEndDate());
            return processRunnerInfoVO;
        }).collect(Collectors.toList());
    }

    @Override
    public Boolean stop(ProcessRunnerStopRequest processRunnerStopRequest) {
        List<ProcessInstance> activeProcessTrans = ProcessEngine.INSTANCE.getActiveProcessTrans();
        //所有正在运行的
        List<ProcessInstance> collect = activeProcessTrans.stream().filter(Pipeline::isRunning).toList();

        if (CollUtil.isEmpty(collect)) {
            return Boolean.FALSE;
        }

        ProcessInstance processInstance = collect.stream().filter(v -> {
            String tid = v.getTid();
            String processId = v.getProcessId();
            String processVersion = v.getProcessVersion();
            return StrUtil.equals(tid, processRunnerStopRequest.getTid())
                    &&
                    StrUtil.equals(processId, processRunnerStopRequest.getProcessId())
                    &&
                    StrUtil.equals(processVersion, processRunnerStopRequest.getProcessVersion());
        }).findFirst().orElse(null);
        if (ObjectUtil.isNull(processInstance)) {
            return Boolean.FALSE;
        }
        processInstance.stopAll();
        processInstance.safeStop();
        return Boolean.TRUE;
    }

    @Override
    public Boolean kill(ProcessRunnerKillRequest processRunnerStopRequest) {

        List<ProcessInstance> activeProcessTrans = ProcessEngine.INSTANCE.getActiveProcessTrans();
        //所有正在运行的
        List<ProcessInstance> collect = activeProcessTrans.stream().filter(Pipeline::isRunning).toList();

        if (CollUtil.isEmpty(collect)) {
            return Boolean.FALSE;
        }

        ProcessInstance processInstance = collect.stream().filter(v -> {
            String tid = v.getTid();
            String processId = v.getProcessId();
            String processVersion = v.getProcessVersion();
            return StrUtil.equals(tid, processRunnerStopRequest.getTid())
                    &&
                    StrUtil.equals(processId, processRunnerStopRequest.getProcessId())
                    &&
                    StrUtil.equals(processVersion, processRunnerStopRequest.getProcessVersion());
        }).findFirst().orElse(null);
        if (ObjectUtil.isNull(processInstance)) {
            return Boolean.FALSE;
        }
        processInstance.stopAll();
        processInstance.safeStop();
        processInstance.kill();
//        processInstance.safeStop();
        return Boolean.TRUE;
    }
}
